package com.itheima.health.service.impl;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.dto.OrderSettingParam;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import org.apache.ibatis.annotations.Select;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderSettingServoceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    /*
    * 预约查询
    * */
    @Override
    public List<OrderSettingParam> listOrder(Integer year, Integer month) {
        //获取前端传过来的年和月
        LocalDate beng = LocalDate.of(year, month,1);
        LocalDate end = beng.withDayOfMonth(beng.lengthOfMonth());
        //根据第一天和最后一天查询
        List<OrderSettingParam> orderSettingParams = new ArrayList<>();
       List<OrderSetting> orderSettingss = orderSettingDao.select(beng,end);
       //遍历查询到的预约详情
        for (OrderSetting settingss : orderSettingss) {
            OrderSettingParam orderSettingParam = new OrderSettingParam();
            BeanUtils.copyProperties(settingss,orderSettingParam);
           orderSettingParam.setDate(settingss.getOrderDate().getDate());
            orderSettingParams.add(orderSettingParam);
        }
        return orderSettingParams;
    }
    /*
     * 设置预约人数
     * */
    @Override
    public void add(OrderSetting orderSetting) {
        //添加之前先查询一下如何存在就直接更改
       OrderSetting orderSetting1 = orderSettingDao.selectDate(orderSetting.getOrderDate());
       if (orderSetting1 == null){
           orderSettingDao.add(orderSetting);
       }else {
           orderSettingDao.update(orderSetting);
       }

    }
/*
* 批量导入预约
* */
    @Override
    public void upload(MultipartFile excelFile) {
    //先获取表
        //创建输入流
        try {
            InputStream in = excelFile.getInputStream();
            Workbook excel = new XSSFWorkbook(in);
            //获取exal文件第一个文件
            Sheet sheet = excel.getSheetAt(0);
            //获取最后一行数据
            int lastRowNum = sheet.getLastRowNum();
            //创建一个集合存储数据
            List<OrderSetting> orderSettinglist = new ArrayList<>();
            //定义一个for循环 遍历elsx表中信息
            for (int i = 1; i <lastRowNum+1 ; i++) {
                //获取sheet页中的行
                Row row = sheet.getRow(i);
                //获取每行中的单元格
                Cell cell = row.getCell(0);
                //判断这个单元格是神么格式
                if (cell !=null){
                    //获取单元格中文本 日期格式
                    Date dateCellValue = cell.getDateCellValue();
                    //获取第二个单元格中的数据
                    Cell cell1 = row.getCell(1);
                    if (cell1 !=null){
                        //获取里面的数据
                        double numericCellValue = cell1.getNumericCellValue();
                        OrderSetting orderSetting = new OrderSetting();
                        orderSetting.setOrderDate(dateCellValue);
                        orderSetting.setNumber((int)numericCellValue);
                        //把获得的对象数据存到集合中
                        orderSettinglist.add(orderSetting);
                    }else {
                        //如果为空就抛出异常
                        throw new BusinessRuntimeException(MessageConst.GET_ORDERSETTING_FAIL);
                    }
                }else {
                    //如果为空就抛出异常
                    throw new BusinessRuntimeException(MessageConst.GET_ORDERSETTING_FAIL);
                }
            }
            //批量导入数据库
            bachadd(orderSettinglist);
            //关闭流
            in.close();
            excel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void bachadd(List<OrderSetting> orderSettinglist) {
        for (OrderSetting orderSetting : orderSettinglist) {
            //先查询是否有数据
          OrderSetting orderSetting1 =  orderSettingDao.selectDate(orderSetting.getOrderDate());
          if (orderSetting1 == null){
              //添加
              orderSettingDao.add(orderSetting);
          }else {
              //修改
              orderSettingDao.update(orderSetting);
          }

        }
    }
}
